<template>
    <el-dialog model-value="dialogVisible" :title="dialogTitle" width="30%" @close="handleClose">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="身份证号" prop="stu_id">
                <el-input v-model="form.stu_id" :disabled="form.stu_id == -1 ?
        false : 'disabled'" />
                <el-alert v-if="form.stu_id" title="默认初始密码：123456" :closable="false" style="line-height: 10px;"
                    type="success">
                </el-alert>
            </el-form-item>
            <el-form-item label="总分" prop="stu_grade_total">
                <el-input v-model="form.stu_grade_total" />
            </el-form-item>
            <el-form-item label="政治成绩" prop="stu_grade_politics">
                <el-input v-model="form.stu_grade_politics" />
            </el-form-item>
            <el-form-item label="专业课成绩" prop="stu_grade_procourse">
                <el-input v-model="form.stu_grade_procourse" />
            </el-form-item>
            <el-form-item label="数学成绩" prop="stu_grade_math">
                <el-input v-model="form.stu_grade_math" />
            </el-form-item>
           
            <el-form-item label="英语成绩" prop="stu_grade_english">
                <el-input v-model="form.stu_grade_english" />
            </el-form-item>
            <el-form-item label="应/历届" prop="stu_fresh_previous">
                <el-input v-model="form.stu_fresh_previous" />
            </el-form-item>
            <el-form-item label="本科专业" prop="stu_graduate_major">
                <el-input v-model="form.stu_graduate_major" />
            </el-form-item>
            <el-form-item label="本科院校" prop="stu_graduate_school">
                <el-input v-model="form.stu_graduate_school" />
            </el-form-item>
            <el-form-item label="研究生专业" prop="stu_major">
                <el-input v-model="form.stu_major" />
            </el-form-item>
            <el-form-item label="性别" prop="stu_gender">
                <el-input v-model="form.stu_gender" />
            </el-form-item>
            <el-form-item label="类型(专/学硕)" prop="stu_type">
                <el-input v-model="form.stu_type" />
            </el-form-item>
            <el-form-item label="入学年份" prop="stu_enrollment_year">
                <el-input v-model="form.stu_enrollment_year" />
            </el-form-item>
            <el-form-item label="姓名" prop="stu_name">
                <el-input v-model="form.stu_name" />
            </el-form-item>
            <el-form-item label="生日" prop="stu_birth">
                <el-input v-model="form.stu_birth" />
            </el-form-item>

            <el-form-item label="政治面貌" prop="stu_politic_outlook">
                <el-input v-model="form.stu_politic_outlook" />
            </el-form-item>
            <el-form-item label="手机号" prop="stu_phone">
                <el-input v-model="form.stu_phone" />
            </el-form-item>
            <el-form-item label="民族" prop="stu_nation">
                <el-input v-model="form.stu_nation" />
            </el-form-item>
            <el-form-item label="爱好" prop="stu_hobby">
                <el-input v-model="form.stu_hobby" type="textarea" :rows="4" />
            </el-form-item>
            <el-form-item label="获得荣誉" prop="stu_honor">
                <el-input v-model="form.stu_honor" type="textarea" :rows="4" />
            </el-form-item>
            <el-form-item label="工作经历" prop="stu_workexperience">
                <el-input v-model="form.stu_workexperience" type="textarea" :rows="4" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button type="primary" @click="handleConfirm">确认</el-button>
                <el-button @click="handleClose">取消</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script setup>
import { defineEmits, defineProps, ref, watch } from "vue";
import { toast } from '@/util/util'

// api
import {getStuItem,saveStu} from "@/api/stu_teacher"


const props = defineProps(
    {
        stu_id: {
            type: String,
            default: '-1',
            required: true
        },
        dialogTitle: {
            type: String,
            default: '',
            required: true
        },
        dialogVisible: {
            type: Boolean,
            default: false,
            required: true
        }
    }
)
const form = ref({
    stu_id: "", // 学号-身份证号
    stu_pwd: "123456", // 初始密码
    stu_phone: 1,  
    stu_nation: "",  //民族
    stu_politic_outlook: "",  // 政治面貌
    stu_birth: "",  // 生日
    stu_name: "",   // 姓名
    stu_enrollment_year: "", // 入学年份
    stu_type: "", // 类型(专/学硕)
    stu_img: "",  // 照片
    stu_gender: "", // 性别
    stu_major: "", //  研究生专业
    stu_graduate_school: "",  // 本科毕业院校
    stu_graduate_major: "",   // 本科毕业专业
    stu_fresh_previous: "",  // 应/历届
    stu_grade_english: "",  // 英语成绩
    stu_grade_math: "",      // 数学成绩
    stu_grade_politics: "", // 政治成绩
    stu_grade_procourse: "",  // 专业课成绩
    stu_grade_total: "",   // 总分
    stu_hobby: "",      // 爱好
    stu_honor: "",      // 荣誉
    stu_workexperience: "", // 工作经历
})

// 定义身份证号验证规则
const validateIdCard = async (rule, value, callback) => {
  const reg = /(^\d{15}$)|(^\d{17}([0-9]|X|x)$)/;
  if (!value) {
    callback(new Error('请输入身份证号'));
  } else if (!reg.test(value)) {
    callback(new Error('请输入正确的身份证号'));
  } else {
    // 18位身份证号校验码验证
    if (value.length === 18) {
      const idCardWi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 加权因子
      const idCardY = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]; // 校验码对应值
      let sum = 0;
      for (let i = 0; i < 17; i++) {
        sum += parseInt(value.charAt(i), 10) * idCardWi[i];
      }
      const lastChar = idCardY[sum % 11].toString().toUpperCase();
      if (value.charAt(17).toUpperCase()!== lastChar) {
        callback(new Error('请输入正确的身份证号！'));
      } else {

        // 检验成功之后-检验是否存在
        if (form.value.stu_id == -1) {
        const res = await getStuItem({
            stu_id:form.value.stu_id
        })
        if (res.data.code == 500) {
            callback(new Error("该身份证号已存在！"));
        } else {
            callback();
        }
    } else {
        callback();
    }
      }
    } else {
      callback();
    }
  }
};

const rules = ref({
    stu_id: [
        { required: true, message: '请输入身份证号' },
        { required: true, validator: validateIdCard, trigger: "blur" }
    ],
    stu_nation: [
        { required: true, message: '请输入民族' ,trigger: "blur"},
    ],
    stu_name: [
        { required: true, message: '请输入姓名' ,trigger: "blur"},
    ],
    stu_type: [
        { required: true, message: '请输入学术类型' ,trigger: "blur"},
    ],
    stu_gender: [
        { required: true, message: '请输入性别' ,trigger: "blur"},
    ],
    stu_graduate_major: [
        { required: true, message: '请输入本科专业' ,trigger: "blur"},
    ],
    stu_grade_english: [
        { required: true, message: '请输入英语成绩' ,trigger: "blur"},
    ],
    stu_grade_math: [
        { required: true, message: '请输入数学成绩' ,trigger: "blur"},
    ],
    stu_grade_procourse: [
        { required: true, message: '请输入专业课成绩' ,trigger: "blur"},
    ],
    stu_major: [
        { required: true, message: '请输入研究生专业' ,trigger: "blur"},
    ],
    stu_phone: [{ required: true, message: "手机号码不能为空", trigger: "blur" }, {
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        message: "请输入正确的手机号码",
        trigger: "blur"
    }],
})

const formRef = ref(null)
const initFormData = async (stu_id) => {
    const res = await getStuItem({
        stu_id:stu_id
    }) 
    form.value = res.data.stu;
}
watch(
    () => props.dialogVisible,
    () => {
        let stu_id = props.stu_id;
        if (stu_id != -1) {
            initFormData(stu_id)
        } else {
            form.value = {
                stu_id: "", // 学号-身份证号
                stu_pwd: "123456", // 初始密码
                stu_phone: 1,  
                stu_nation: "",  //民族
                stu_politic_outlook: "",  // 政治面貌
                stu_birth: "",  // 生日
                stu_name: "",   // 姓名
                stu_enrollment_year: "", // 入学年份
                stu_type: "", // 类型(专/学硕)
                stu_img: "",  // 照片
                stu_gender: "", // 性别
                stu_major: "", //  研究生专业
                stu_graduate_school: "",  // 本科毕业院校
                stu_graduate_major: "",   // 本科毕业专业
                stu_fresh_previous: "",  // 应/历届
                stu_grade_english: "",  // 英语成绩
                stu_grade_math: "",      // 数学成绩
                stu_grade_politics: "", // 政治成绩
                stu_grade_procourse: "",  // 专业课成绩
                stu_grade_total: "",   // 总分
                stu_hobby: "",      // 爱好
                stu_honor: "",      // 荣誉
                stu_workexperience: "", // 工作经历
            }
        }
    }
)
const emits = defineEmits(['update:modelValue', 'initStuList'])
const handleClose = () => {
    emits('update:modelValue', false)
}
const handleConfirm = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            let result = await saveStu(form.value)
            let data = result.data;
            if (data.code == 200) {
                toast("执行成功！")
                formRef.value.resetFields();
                emits("initStuList")
                handleClose();
            } else {
                toast(data.msg,'error')
            }
        } else {
            console.log("fail")
        }
    })
}
</script>
<style scoped>
</style>